<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\CityList;
use App\Models\OpenCity;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class OpenCityController extends Controller
{
    use HasResourceActions;
    private $name = '开拓城市';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OpenCity);

        $grid->id('Id');
        $grid->key('Key');
        $grid->City()->name('城市名称');
        $grid->remark('备注');
        $grid->default('默认城市')->display(function ($default) {
            return $default ? '是' : '否';
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OpenCity::findOrFail($id));

        $show->id('Id');
        $show->key('Key');
        $show->City('城市名称')->unescape()->as(function ($city) {
            return $city->name;
        });
        $show->remark('备注');
        $show->default('默认城市')->unescape()->as(function ($default) {
            return $default ? '是' : '否';
        });

        $show->created_at('创建时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new OpenCity);

        $form->select('key', '地区')->options(CityList::getCityList())->rules('required');

        $form->text('remark', 'Remark');
        $states = [
            'on' => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->switch('default', '默认城市')->default(0)->states($states);

        return $form;
    }
}
